window.onload = function() {
	pineza.clickedNote = false;
	pineza.clickNoteTimer = false;
	pineza.editNoteOn = false;
	pineza.minSizeX = 90;
	pineza.maxSizeX = 1000;
	pineza.minSizeY = 40;
	pineza.maxSizeY = 500;
	if (isSet(pineza.board))
		refreshBoardHTML(pineza.board);

	var x = parent.document.getElementById('searchSection');
	if (x && (x.innerHTML == '')) {
		x.innerHTML = '<div>' +
			'<input id="search" type="text" maxlength="256" size="30" ' +
			'class="searchField" onkeyup="return parent.searchAuto(event, this);" />' +
			'<button class="searchField" onclick="return parent.searchNow();">' +
			'Search</button></div>';
		x = parent.document.getElementById('search');
		x.focus();
	}
}

function pickNote(e, note) {
	if (!e) e = window.event;
	if (e.stopPropagation) {
		e.stopPropagation();
	} else {
		e.cancelBubble = true;
	} 

	var n = getelid('s' + note);
	if (!n) return false;

	var nl = parent.pineza.pickedNotes;
	if (note in nl) {
		n.style.backgroundImage = 'none';
		n.style.backgroundRepeat = 'no-repeat';
		delete nl[note];
	}
	else {
		nl[note] = true;
		n.style.backgroundImage = 'url(' + pineza.server + 'images/picked.jpg)';
		n.style.backgroundRepeat = 'repeat';
	}
}

function resetKimeno(f, note) {
	if (!note) f.kimeno.value = '';
	f.kimeno.style.backgroundImage = 'url(' + pineza.server + 'images/kare.gif)';
	f.kimeno.style.backgroundRepeat = 'repeat';
	f.kimeno.style.fontFamily = 'monospace';
	f.kimeno.focus();
	return false;
}

function cancelKimeno(f, note) {
	pineza.editNoteOn = false;
	f.style.display = 'none';
	if (note) {
		var x = getelid('s' + note);
		if (x) {
			x.justRestored = true;
			if (x.style)
				x.style.display = '';
		}
	}

	return false;
}

function setColor(color, obj) {
	var f = getelid('formaNote');
	if (!f) return false;

	f.color.value = color;
	f.kimeno.style.backgroundImage = 'none';
	f.kimeno.style.backgroundColor = '#' + color;
	f.kimeno.focus();
	obj.innerHTML = '<img src="' + pineza.server + 'images/working.gif" ' +
		'alt="working icon" style="width: 0.6cm; padding: 0.15cm;" />';
	setTimeout(function() {
		f.localTime.value = currentTimestamp(true);
		f.submit();
	}, 100);
	return false;
}

function setFont(font) {
	var f = getelid('formaNote');
	if (!f) return;

	f.font.value = font;
	f.kimeno.style.fontFamily = font;
	f.kimeno.focus();
	return false;
}

function clickSimioma(e, note) {
	if (pineza.editNoteOn)
		return false;

	var s = getelid('s' + note);
	pineza.clickNoteTimer = setTimeout(function() {
		pineza.clickNoteTimer = false;
		var s = getelid('s' + note);
		if (s.justRestored) {
			s.justRestored = false;
			return;
		}

		var soma = getelid('k' + note);
		if (soma.style && (soma.style.width || soma.style.height)) {
			soma.style.minWidth = '';
			soma.style.maxWidth = '';
			soma.style.width = '';
			soma.style.minHeight = '';
			soma.style.maxHeight = '';
			soma.style.height = '';
			soma.className = '';
		}
		switch (soma.className) {
		case 'soma':
			soma.className = 'soma somaWide';
			noteSize(note, 'WIDE');
			soma.title = 'Click to enlarge farther';
			break;
		case 'soma somaWide':
			soma.className = 'soma somaBig';
			noteSize(note, 'BIG');
			soma.title = 'Click to restore normal size';
			break;
		default:
			soma.className = 'soma';
			noteSize(note, 'NORMAL');
			soma.title = 'Click to enlarge';
			break;
		}

		soma.title += ', double click to edit';
	}, 300);

	s.justRestored = false;
}

function editSimioma(e, note) {
	if (pineza.clickNoteTimer) {
		clearTimeout(pineza.clickNoteTimer);
		pineza.clickNoteTimer = false;
	}

	if (!e) e = window.event;
	if (e.stopPropagation) {
		e.stopPropagation();
	} else {
		e.cancelBubble = true;
	} 

	if (!pineza.login) {
		alert('Please login first in order to edit the note.');
		return false;
	}

	var s = getelid('s' + note);
	var f = getelid('formaNotePosition');
	if ((!f) || (!s)) {
		return false;
	}

	s.justRestored = true;
	pineza.editNoteOn = true;
	var fn = formaNoteHTML(note);
	if (fn.match(/^ERROR@/)) {
		f.innerHTML = fn.replace(/^ERROR@/, '<div class="lathos">') + '</div>';
		setTimeout(function() {
			f.innerHTML = '';
		}, 1000);
			
	}
	else if (fn) {
		f.innerHTML = fn;
		f = getelid('formaNote');
		if (f) f.kimeno.focus();
	}
	return false;
}

function noteSize(note, size) {
	refreshBoardHTML(pineza.board);
	var req = newRequest('noteSize');
	req.open('post', pineza.server + 'board/noteSize.php', true);
	req.onreadystatechange = function() {
		if (req.readyState != 4)
			return;

		if (req.status != 200) {
			alert('noteSize: AJAX request failed (status = ' + req.status + ')');
			return;
		}
	};

	var params = 'login=' + uri(pineza.login) + '&note=' + uri(note);
	var n = getelid('k' + note);
	if (n && n.style && (n.style.width || n.style.height)) {
		if (n.style.width)
			params += '&width=' + uri(parseInt(n.style.width));
		if (n.style.height)
			params += '&height=' + uri(parseInt(n.style.height));
		n.title = 'Click to restore normal size, double click to edit';
	}
	else if (size) {
		params += '&size=' + uri(size);
	}
	else {
		params += '&size=NORMAL';
	}

	sendRequest(req, params);
}

function neoSimioma(obj) {
	if (obj != undefined) {
		var cnt = 0;
		while (isSet(obj) && isSet(obj.parentNode)) {
			if (++cnt > 100)
				break;

			obj = obj.parentNode;
			if (isSet(obj.id) && obj.id.match(/^s[0-9]/)) {
				obj.justRestored = true;
				break;
			}
		}
	}

	if (pineza.editNoteOn) return false;

	pineza.editNoteOn = true;
	newNote(parent);
}

/*
Η function που ακολουθεί καλείται στο "onmouseover" της αριστερής
εργαλειοθήκης των σημειωμάτων. Πρόκειται για την εργαλειοθήκη που
περιέχει το εικονίδιο των σημαντικών σημειωμάτων. Κατά το "mouseover",
λοιπόν σ' αυτήν την εργαλειοθήκη, εμφανίζεται η εργαλειοθήκη,
η οποία συνήθως παραμένει κρυφή για να μην κουράζει το μάτι.
*/

function showNTBL(note) {
	var x = getelid('ntbl' + note);
	if (x && x.style) {
		x.style.display='table';
	}
}

/*
Η function που ακολουθεί καλείται κατά το "mouseout" της αριστερής
εργαλειοθήκης των σημειωμάτων. Στόχος είναι η απόκρυψη της εργαλειοθήκης,
εκτός και αν το σημείωμα είναι σημαντικό, οπότε παραμένει φανερή
και αυτό έχει ως αποτέλεσμα να φαίνεται το εικονίδιο σημαντικού
μηνύματος και γενικότερα η εργαλειοθήκη (ασήμαντο bug).
*/

function hideNTBL(note) {
	var x = getelid('s' + note);
	if (x && (x.style.borderColor != 'red')) {
		x = getelid('ntbl' + note);
		x.style.display = 'none';
	}
}

function importantNote(note) {
	var x = getelid('s' + note);
	x.justRestored = true;

	if (x.style.borderColor != 'red') {
		x.style.borderColor = 'red';
	}
	else {
		x.style.borderColor = '#999966';
	}

	cleanupBoardHTML(pineza.board);
	var req = newRequest('setPriority');
	req.open('post', pineza.server + 'board/setPriority.php', true);
	req.onreadystatechange = function() {
		if (req.readyState != 4)
			return;

		if (req.status != 200) {
			alert('setPriority: AJAX request failed (status = ' + req.status + ')');
			return;
		}
	};
	var params = 'login=' + uri(pineza.login) + '&note=' + uri(note) +
		'&priority=' + (x.style.borderColor == 'red' ? 'HIGH' : 'NORMAL');
	sendRequest(req, params);
}

function mailNote(note, icon) {
	var x = getelid('s' + note);
	x.justRestored = true;

	if (!pineza.login) {
		alert('Please login first in order to mail the note.');
		return false;
	}

	var errmsg = '';
	while (true) {
		var email = prompt(errmsg + 'After mailing to yourself, the note will be moved to ' +
			'trash. If mailed to another address the note will not be moved.\n\n' +
			'Please enter a valid email address:', pineza.myEmail);
		if (email === null) return false;
		if (validEmail(email)) break;
		errmsg = email + ' is not a valid email adress. Please provide a valid email address.\n\n';
	}

	var img = getelid('st' + note);
	if (img) {
		img.innerHTML = '<div style="font-size: 0.8em; font-style: italic; ' +
			'margin-top: -1.8em; position: absolute;' +
			'border-style: dotted; border-width: 1px; padding: 2px;">' +
			'Mailing…&nbsp;<img src="' + pineza.server +
			'images/working.gif" alt="mailing icon" style="width: 1.4em;" /></div>';
		img.style.display = 'inline';
	}

	var req = newRequest('mailNote');
	req.open('post', pineza.server + 'board/mailNote.php', true);
	req.onreadystatechange = function() {
		if (req.readyState != 4)
			return;

		if (req.status != 200) {
			alert('mailNote: AJAX request failed (status = ' + req.status + ')');
			return;
		}

		var rsp = fixResponse(req);
		if (rsp === 'SELFMAIL') {
			deleteNote(note);
		}
		else if (rsp) {
			alert('mail failed (' + rsp + ')');
		}

		if (img) {
			img.innerHTML = '';
			img.style.display = 'none';
		}
	};
	var params = 'login=' + uri(pineza.login) + '&note=' + uri(note) +
		'&email=' + uri(email);
	sendRequest(req, params);
}

function deleteNote(note) {
	var x = getelid('s' + note);
	x.justRestored = true;

	if (!pineza.login) {
		alert('You must login first in order to delete notes.');
		return false;
	}

	var sbf = parent.document.getElementById('boardSB');
	if ((!sbf) || (!sbf.contentWindow) || (!sbf.contentWindow.pineza)) {
		alert('Cannot access "boardSB" iframe or its properties');
		return false;
	}

	workingBoardSB(pineza.board);
	workingBoardSB('TRASH');
	delete sbf.contentWindow.pineza.boardHTML[pineza.board];
	delete sbf.contentWindow.pineza.boardHTML['TRASH'];
	setTimeout(function() {
		moveToTrash(note);
	}, 10);
}

function moveToTrash(note) {
	pineza.clickedNote = false;
	if (pineza.clickNoteTimer)
		clearTimeout(pineza.clickNoteTimer);

	var nt = getelid('n' + note);
	if (nt && nt.parentNode) {
		nt.parentNode.removeChild(nt);
	}
	var req = newRequest('moveToTrash');
	req.open('post', pineza.server + 'board/moveNotes.php', true);
	req.onreadystatechange = function() {
		if (req.readyState != 4)
			return;

		if (req.status != 200) {
			alert('moveToTrash: AJAX request failed (status = ' + req.status + ')');
			return;
		}

		refreshBoardSB('TRASH');
		refreshBoardSB(pineza.board);
		refreshBoardHTML(pineza.board);
	};
	var params = 'login=' + uri(pineza.login) + '&notes=' + uri(note + '@') +
		'&board=' + uri('TRASH');
	sendRequest(req, params);
}

/*
Η function `setTitle' διευκολύνει την εισαγωγή τίτλων στα σημειώματα.
Πράγματι, κάνοντας κλικ στη λέξη "titles" στο επεξηγηματικό κείμενο
που βρίσκεται στο αριστερό μέρος της φόρμας εισαγωγής και ενημέρωσης
σημειωμάτων, όλο το κείμενο που έχει περαστεί μέχρι στιγμής μετατρέπεται
σε τίτλο, δηλαδή εγγράφεται ανάμεσα σε tags "<h>" και "</h>".
Αν το κείμενο ήδη περιέχει tags τίτλου, τότε απογυμνώνεται από αυτά,
δηλαδή καταργείται ο τίτλος του σημειώματος.
*/

function setTitle() {
	var f = getelid('formaNote');
	if (f && f.kimeno && f.kimeno.value) {
		if (f.kimeno.value.match(/<h/))
			f.kimeno.value = f.kimeno.value.replace(/<\/?h[0-9]?>/g, '');
		else
			f.kimeno.value = '<h>' + f.kimeno.value + '</h>';
		f.kimeno.focus();
		/*
		Η παρακάτω γραμμή μεταφέρει τον cursor στο τέλος του κειμένου
		σε ορισμένους browsers.
		*/
		f.kimeno.value = f.kimeno.value;
	}

	return false;
}

function resizeStart(e, note, dirX, dirY) {
	if (!e) e = window.event;
	if (e.pageX) {
		pineza.mouseX0 = e.pageX;
		pineza.mouseY0 = e.pageY;
	}
	else {
		pineza.mouseX0 = e.clientX + document.body.scrollLeft +
			document.documentElement.scrollLeft;
		pineza.mouseY0 = e.clientY + document.body.scrollTop +
			document.documentElement.scrollTop;
	}

	document.onmousemove = function(e) {
		resizeMove(e, note, dirX, dirY);
	};

	document.onmouseup = function(e) {
		resizeEnd(e, note, dirX, dirY);
	};

	pineza.resizeJustClicked = true;
	setTimeout(function() {
		pineza.resizeJustClicked = false;
	}, 10);
}

function resizeMove(e, note, dirX, dirY) {
	if (isSet(pineza.resizeJustClicked) && pineza.resizeJustClicked)
		return;
	if (!e) {
		e = window.event;
	}
	if (e.pageX) {
		pineza.mouseX = e.pageX;
		pineza.mouseY = e.pageY;
	}
	else {
		pineza.mouseX = e.clientX + document.body.scrollLeft +
			document.documentElement.scrollLeft;
		pineza.mouseY = e.clientY + document.body.scrollTop +
			document.documentElement.scrollTop;
	}

	var dx = pineza.mouseX - pineza.mouseX0;
	var dy = pineza.mouseY - pineza.mouseY0;

	pineza.mouseX0 = pineza.mouseX;
	pineza.mouseY0 = pineza.mouseY;

	var ul = getelid('k' + note);
	if (ul && ul.style && ul.style.width) {
		var sizeX = parseInt(ul.style.width);
	}
	else {
		var ulXY = apolitaXY(ul);
		var lr = getelid('XY' + note);
		var lrXY = apolitaXY(lr);
		sizeX = lrXY.x - ulXY.x;
	}

	if (ul && ul.style && ul.style.height) {
		var sizeY = parseInt(ul.style.height);
	}
	else {
		var ulXY = apolitaXY(ul);
		var lr = getelid('XY' + note);
		var lrXY = apolitaXY(lr);
		sizeY = lrXY.y - ulXY.y;
	}

	if (dirX) {
		sizeX += dx;
		if ((sizeX >= pineza.minSizeX) && (sizeX <= pineza.maxSizeX)) {
			ul.style.maxWidth = sizeX + 'px';
			ul.style.minWidth = sizeX + 'px';
			ul.style.width = sizeX + 'px';
		}
	}

	if (dirY) {
		sizeY += dy;
		if ((sizeY >= pineza.minSizeY) && (sizeY <= pineza.maxSizeY)) {
			ul.style.maxHeight = sizeY + 'px';
			ul.style.minHeight = sizeY + 'px';
			ul.style.height = sizeY + 'px';
		}
	}
}

function resizeEnd(e, note, dirX, dirY) {
	document.onmousemove = null;
	document.onmouseup = null;
	noteSize(note, false);

}

function cleanupBoardHTML(board) {
	var sbf = parent.document.getElementById('boardSB');
	if (sbf && sbf.contentWindow && sbf.contentWindow.pineza &&
		sbf.contentWindow.pineza.boardHTML)
		delete sbf.contentWindow.pineza.boardHTML[board];
}

function refreshBoardHTML(board) {
	var b = getelid('boardNotes');
	if (!b) return;

	var sbf = parent.document.getElementById('boardSB');
	if (sbf && sbf.contentWindow && sbf.contentWindow.pineza &&
		sbf.contentWindow.pineza.boardHTML)
		sbf.contentWindow.pineza.boardHTML[board] = b.innerHTML;
}
